package helpers;

import java.util.ArrayList;
import java.util.Locale;
import java.util.ResourceBundle;
import java.util.regex.Pattern;

import javax.servlet.http.HttpServletRequest;

public class UrlHelper{
	public static String getServerUrl(HttpServletRequest request) {
		String url = "";
		String serverName = request.getServerName();
		String port = Integer.toString(request.getServerPort());
		if (serverName.equals("localhost")) {
			url += "http://" + serverName + ":" + port;
		} else {
			url += "http://" + serverName;
		}
		return url;
	}
	
	public static String getDomainName(String url) {
		String regex = "([a-zA-Z0-9-]+[.])+[a-zA-z]+";
		String domainName = StringHelper.findString(regex, url).replace("www.", "");
		return domainName;
	}
	
	public static String getName(String urlString) {
		String[] subStr = urlString.split("/");
		return subStr[subStr.length - 1];
	}
	
	public static String fomatSitemap(String url)
	{
		if (url != null)
			url = "http://"+url.toLowerCase().replaceAll("(http|https)+://", "").trim().replaceAll("[/]+$", "");
		return url;
	}
	
	public static String fomatDmains(String url)
	{
		if (url != null)
			url = url.toLowerCase().replaceAll("(http|https)+://", "")
					.replaceAll("^[\\w\\W]+[\\/]{2}", "")
					.replaceAll("[\\/]+[\\w\\W]*$", "")
					.replaceAll("[\\?]+[\\w\\W]*$", "")
					.replaceAll("[ !@#$%^&*?()\\[\\]{}+=,:;|\\\'\\\"/\\\\`~]+", "")
					.trim();

		return url;
	}
}